from django.utils.safestring import mark_safe
from django.shortcuts import HttpResponse
from stark.service.v1 import StarkHandler, StarkModelForm
from django.urls import re_path
from web import models


# 添加记录默认只显示此客户的跟进内容
class ConsultRecordModelForm(StarkModelForm):
    class Meta:
        model = models.ConsultRecord
        fields = ['note', ]


class ConsultRecordHandler(StarkHandler):
    field_name_list = ['note', 'consultant', 'date']
    model_form_class = ConsultRecordModelForm

    def display_edit_del(self, obj=None, is_header=None, *args, **kwargs):
        """编辑删除 在一列显示"""
        if is_header:
            return '操作'
        customer_id = kwargs.get('customer_id')
        tmp = '<a href="%s" class="glyphicon glyphicon-edit" style="color:darkgrey"></a> &nbsp;&nbsp;<a href="%s" ' \
              'class="glyphicon glyphicon-trash" style="color:red"></a>' % (
                  self.reverse_edit_url(pk=obj.pk, customer_id=customer_id),
                  self.reverse_del_url(pk=obj.pk, customer_id=customer_id))
        return mark_safe(tmp)

    def get_urls(self):
        patterns = [
            re_path('list/(?P<customer_id>\d+)/', self.wrapper(self.changelist_view), name=self.get_list_url_name),
            re_path('add/(?P<customer_id>\d+)/', self.wrapper(self.add_view), name=self.get_add_url_name),
            re_path('edit/(?P<customer_id>\d+)/(?P<pk>\d+)/', self.wrapper(self.edit_view),
                    name=self.get_edit_url_name),
            re_path('del/(?P<customer_id>\d+)/(?P<pk>\d+)/', self.wrapper(self.del_view), name=self.get_del_url_name),
        ]
        patterns.extend(self.extra_urls())  # 新增的url 扩增
        return patterns

    def get_queryset(self, request, *args, **kwargs):
        customer_id = kwargs.get('customer_id')
        current_user_id = request.session['current_user_id']
        return self.model_class.objects.filter(customer_id=customer_id, customer__consultant_id=current_user_id)

    def save(self, request, form, is_update, *args, **kwargs):
        is_update = False
        customer_id = kwargs.get('customer_id')
        current_user_id = request.session.get('current_user_id')
        obj = models.Customer.objects.filter(id=customer_id, consultant_id=current_user_id).exists()
        if not obj:
            return HttpResponse('非法操作')
        if not is_update:
            """保存默认值"""
            form.instance.customer_id = customer_id
            form.instance.consultant_id = current_user_id
        form.save()

    def get_edit_object(self, request, pk, *args, **kwargs):
        """重写父类方法，防止浏览器地址栏修改不是自己的记录"""
        customer_id = kwargs.get('customer_id')
        current_user_id = request.session.get('current_user_id')
        return models.ConsultRecord.objects.filter(pk=pk,
                                                   customer_id=customer_id,
                                                   consultant_id=current_user_id).first()

    def get_del_object(self, request, pk, *args, **kwargs):
        customer_id = kwargs.get('customer_id')
        current_user_id = request.session.get('current_user_id')
        del_obj = models.ConsultRecord.objects.filter(pk=pk,
                                                      customer_id=customer_id,
                                                      consultant_id=current_user_id)
        if not del_obj.exists():
            return HttpResponse('要删除的记录不存在')
        del_obj.delete()
